package com.myCommunity.ui;

import java.io.IOException;
import java.net.InetAddress;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myCommunity.dao.impl.ForumDaoImpl;
import com.myCommunity.dao.impl.TopicDaoImpl;
import com.myCommunity.entity.Forum;
import com.myCommunity.entity.Topic;
import com.myCommunity.entity.Users;

public class KeynoteServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Forum> list = new ForumDaoImpl().getForumLevel();
		List<Forum> children = new ArrayList<Forum>();
		for(Forum f : list){
			for(Forum m : f.getChildren())
				children.add(m);
		}
		request.setAttribute("forum", children);
		
		request.getRequestDispatcher("/afterLogin/keynote.jsp").forward(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Users user = (Users) request.getSession().getAttribute("user");
		int forumId = Integer.parseInt(request.getParameter("forum"));
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		Date currentDate = new Date(System.currentTimeMillis());
		String ip = InetAddress.getLocalHost().getHostAddress();
		Topic topic = new Topic(0, title, content, currentDate, forumId, ip, 0, currentDate, 0, 0, 1);
		topic.setUser(user);
		
		new TopicDaoImpl().add(topic);
		
		response.sendRedirect("ForumServlet?forumId="+forumId);
	}
}
